package me.xue.communityhealth.repo;

import com.github.pagehelper.Page;
import me.xue.communityhealth.data.RelationMapping;
import me.xue.communityhealth.model.Staff;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface StaffRepository {

    @Insert("INSERT INTO `staff`(name,phone,entryDate,post,department,number,registeredType,address,gender,age,portrait) " +
            "VALUES(#{name},#{phone},#{entryDate},#{post},#{department},#{number},#{registeredType},#{address},#{gender},#{age},#{portrait})")
    int save(Staff staff);

    @Update("UPDATE `staff` " +
            "SET `name`=#{name},`phone`=#{phone},`entryDate`=#{entryDate},`post`=#{post},`department`=#{department},`number`=#{number},`registeredType`=#{registeredType},`address`=#{address},`gender`=#{gender},`age`=#{age},`portrait`=#{portrait} " +
            "WHERE `id`=#{id}")
    int update(Staff staff);

    @Delete("DELETE FROM `staff` WHERE `id`=#{id}")
    int delete(int id);

    @Select("SELECT * FROM `staff` WHERE `id`=#{id}")
    Staff findOne(int id);

    @Select("SELECT * FROM `staff`")
    @Results({
            @Result(column = "registeredType", property = "registration",
                    one = @One(select = RelationMapping.STAFF_ONE_TO_REGISTRATION))
    })
    Page<Staff> pagedStaffs();

}
